ProgressBar进度条
string filepath = @"E:\Adobe Photoshop CS6.zip";
string filename = Path.GetFileName(filepath);
Console.WriteLine("Start Reading : {0}", filename);
FileStream fs = new FileStream(filepath, FileMode.Open); //open file
long count = fs.Length;//文件长度
int start = 0;//开始读取
int num = 0; //每次读取的长度
long end = count;//剩余读取长度
double prePercent = 0;
ConsoleColor colorBack = Console.BackgroundColor;
ConsoleColor colorFore = Console.ForegroundColor;
if (count > 0)
{
//绘制读条背景界面
Console.WriteLine("********************* Loading *********************");
Console.BackgroundColor = ConsoleColor.DarkCyan;
for (int i = 0; i <= 50; )
{
Console.Write(" ");
}
Console.WriteLine(" ");
Console.BackgroundColor = colorBack;
Console.WriteLine("0%");
Console.WriteLine("***************************************************");
}
byte[] bytes = new byte[1024*10];
int maxlength = bytes.Length;
while (end > 0)
{
fs.Position = start;
num = 0;
if (end < maxlength)
num = fs.Read(bytes, 0, Convert.ToInt32(end));
else
num = fs.Read(bytes, 0, maxlength);
if (num == 0)
break;
start = num;
end -= num;
double percent;
if (start <= count)
{
percent = (double)start / count;
percent = Math.Ceiling(percent * 100);
}else
{
percent = 1;
percent = Math.Ceiling(percent * 100);
}
for (int i = Convert.ToInt32(prePercent); i <= percent; i )
{
//绘制进度条进度
Console.BackgroundColor = ConsoleColor.Yellow;//设置进度条颜色
Console.SetCursorPosition(i/2, 2);//设置光标位置,参数为第几列和第几行
Console.Write(" ");//移动进度条
Console.BackgroundColor = colorBack;//恢复输出颜色
//更新进度百分比,原理同上.
Console.ForegroundColor = ConsoleColor.Green;
Console.SetCursorPosition(0, 3);
Console.Write("{0}% ", i);
Console.ForegroundColor = colorFore;
}
prePercent = percent;
}
fs.Close();
Console.WriteLine(" Finished Reading !");
Console.ReadLine();
评论